package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.topboya.linker.R;
import com.topboya.linker.model.response.DictionaryResponse;
import com.topboya.linker.model.response.TencentLocationResponse;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapAddressAdapter extends RecyclerView.Adapter<MapAddressAdapter.ListItemViewHolder> {

    private static final String TAG = "SelectFileAdapter:";
    private  List<TencentLocationResponse.PoisList> poisLists;
    private Context mContext;
    //   用来保存被选中的对象
    private LinkedHashMap<String, TencentLocationResponse.PoisList> mSelectedPositions = new LinkedHashMap<>();
    public MapAddressAdapter(Context mContext,  List<TencentLocationResponse.PoisList> poisLists) {

        this.poisLists = poisLists;
        this.mContext = mContext;
    }


    //获得选中条目的结果
    public ArrayList<TencentLocationResponse.PoisList> getSelectedItem() {

        ArrayList<TencentLocationResponse.PoisList> selectList = new ArrayList<>();
        for (Map.Entry<String,TencentLocationResponse.PoisList> entry : mSelectedPositions.entrySet()) {

            TencentLocationResponse.PoisList productDetailInfoList = entry.getValue();
            selectList.add(productDetailInfoList);

        }
        return selectList;
    }
    //设置给定位置条目的选择状态
    public void setItemChecked(String id, TencentLocationResponse.PoisList feekbackDataList) {
        mSelectedPositions.clear();
        mSelectedPositions.put(id, feekbackDataList);
    }

    //设置给定位置条目的选择状态
    public void setItemUnChecked(String id) {

        mSelectedPositions.remove(id);
    }

    //根据位置判断条目是否选中
    private TencentLocationResponse.PoisList isItemChecked(int position) {

        return mSelectedPositions.get(poisLists.get(position).getId());
    }
    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_address_item, viewGroup, false);
        return new MapAddressAdapter.ListItemViewHolder(itemView);
    }



    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {


        holder.tv_address_detail.setText(poisLists.get(i).getTitle());
        holder.tv_address.setText(poisLists.get(i).getAddress());
        if (null != isItemChecked(i)) {

            holder.tv_address_detail.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));
        } else {

            holder.tv_address_detail.setTextColor(Color.BLACK);
        }
        holder.itemView.setOnClickListener(v -> {
            if (null != isItemChecked(i)) {

                setItemUnChecked(poisLists.get(i).getId());

            } else {

                setItemChecked(poisLists.get(i).getId(),poisLists.get(i));
            }
            notifyDataSetChanged();
            if (null !=itemClickListener){

                itemClickListener.onClick(holder.itemView ,i);
            }
        });
    }

    @Override
    public int getItemCount() {

        return poisLists == null ? 0 : poisLists.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_address_detail;
        TextView tv_address;

        ListItemViewHolder(View view) {

            super(view);
            this.tv_address_detail = view.findViewById(R.id.tv_address_detail);
            this.tv_address = view.findViewById(R.id.tv_address);

        }

    }

    private MapAddressAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(MapAddressAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

    public interface  OnItemClickListener{

        void onClick(View v, int position );
    }

}
